#include <QLineEdit>
#include <QLabel>
#include <Resources.h>
#include <Configuration.h>
#include <Constants.h>
#include "MessageToolBar.h"

namespace Domy {

/**
 * @brief The MessageFuncKey enum
 * @note  消息功能key
 */
enum MessageFuncKey {
    MsgFunc_Export = 0,
    MsgFunc_Clear,
    MsgFunc_ZoomIn,
    MsgFunc_ZoomOut,
    MsgFunc_ZoomReset,
    MsgFunc_Find,
    MsgFunc_Case,
    MsgFunc_Whole,
    MsgFunc_Prev,
    MsgFunc_Next
};

MessageToolBar::MessageToolBar(QWidget *parent)
    : QToolBar(parent),
      m_label_zoom(new QLabel("100%")),
      m_le_find(new QLineEdit(this)),
      m_is_case_sensitive(false),
      m_is_whole_word(false)
{
    this->setIconSize(QSize(16, 16));

    // action
    QAction *act = this->addAction(GET_ICON("save"), tr("Export"));
    act->setData(static_cast<int>(MsgFunc_Export));

    act = this->addAction(GET_ICON("msg_clear"), tr("Clear"));
    act->setData(static_cast<int>(MsgFunc_Clear));

    this->addSeparator();

    act = this->addAction(GET_ICON("zoom_in"), tr("Zoom In"));
    act->setData(static_cast<int>(MsgFunc_ZoomIn));

    act = this->addAction(GET_ICON("zoom_out"), tr("Zoom In"));
    act->setData(static_cast<int>(MsgFunc_ZoomOut));

    m_label_zoom->setFixedWidth(40);
    m_label_zoom->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    setZoomValueDisplay(domy_config->getSettingsValue(SettingsGroup_Message, SettingsKey_MessageViewZoom).toInt());
    this->addWidget(m_label_zoom);

    QWidget *spacer = new QWidget(this);
    spacer->setFixedWidth(5);
    this->addWidget(spacer);

    act = this->addAction(GET_ICON("zoom_reset"), tr("Zoom Reset"));
    act->setData(static_cast<int>(MsgFunc_ZoomReset));

    this->addSeparator();

    // find
    QLabel *label_find = new QLabel(tr(" Find: "), this);
    this->addWidget(label_find);

    m_le_find->setPlaceholderText(tr("Search for ..."));
    m_le_find->setFixedWidth(200);
    m_le_find->setClearButtonEnabled(true);
    this->addWidget(m_le_find);

    act = this->addAction(GET_ICON("find"), tr("Find"));
    act->setData(static_cast<int>(MsgFunc_Find));

    this->addSeparator();

    act = this->addAction(GET_ICON("prev"), tr("Previous"));
    act->setData(static_cast<int>(MsgFunc_Prev));

    act = this->addAction(GET_ICON("next"), tr("Next"));
    act->setData(static_cast<int>(MsgFunc_Next));

    this->addSeparator();

    act = this->addAction(GET_ICON("case"), tr("Case Sensitive"));
    act->setCheckable(true);
    act->setData(static_cast<int>(MsgFunc_Case));

    act = this->addAction(GET_ICON("whole"), tr("Whole Words Only"));
    act->setCheckable(true);
    act->setData(static_cast<int>(MsgFunc_Whole));

    connect(this, SIGNAL(actionTriggered(QAction *)),
            this, SLOT(slotOnActionTriggered(QAction *)));
    connect(m_le_find, SIGNAL(textChanged(const QString &)),
            this, SLOT(slotOnFindTextChanged(const QString &)));
    connect(m_le_find, SIGNAL(returnPressed()),
            this, SLOT(slotOnFindTextEditFinished()));
}

MessageToolBar::~MessageToolBar()
{

}

void MessageToolBar::setZoomValueDisplay(int zoom)
{
    m_label_zoom->setText(QString("%1%").arg(zoom * 10));
}

bool MessageToolBar::isCaseSensitive() const
{
    return m_is_case_sensitive;
}

bool MessageToolBar::isWholeWord() const
{
    return m_is_whole_word;
}

void MessageToolBar::setFindText(const QString &text)
{
    m_le_find->setText(text);
    m_find_text = text;
}

QString MessageToolBar::getFindText() const
{
    return m_le_find->text();
}

void MessageToolBar::slotOnActionTriggered(QAction *action)
{
    MessageFuncKey key = static_cast<MessageFuncKey>(action->data().toInt());
    switch (key) {
    case MsgFunc_Export:
        action->setData(static_cast<int>(MsgAct_Export));
        emit signalMsgActionTriggered(action);
        break;
    case MsgFunc_Clear:
        action->setData(static_cast<int>(MsgAct_Clear));
        emit signalMsgActionTriggered(action);
        break;
    case MsgFunc_ZoomIn:
        action->setData(static_cast<int>(MsgAct_ZoomIn));
        emit signalMsgActionTriggered(action);
        break;
    case MsgFunc_ZoomOut:
        action->setData(static_cast<int>(MsgAct_ZoomOut));
        emit signalMsgActionTriggered(action);
        break;
    case MsgFunc_ZoomReset:
        action->setData(static_cast<int>(MsgAct_ZoomReset));
        emit signalMsgActionTriggered(action);
        break;
    case MsgFunc_Find:
    {
        QString find_text = m_le_find->text();

        if (!find_text.isEmpty()) {
            m_find_text = find_text;
            emit signalFind(find_text, m_is_case_sensitive, m_is_whole_word);
        }
    }
        break;
    case MsgFunc_Case:
        m_is_case_sensitive = action->isChecked();
        break;
    case MsgFunc_Whole:
        m_is_whole_word = action->isChecked();
        break;
    case MsgFunc_Next:
    {
        QString find_text = m_le_find->text();
        if (!find_text.isEmpty()) {
            emit signalFindNext(find_text, m_is_case_sensitive, m_is_whole_word);
        }
    }
        break;
    case MsgFunc_Prev:
    {
        QString find_text = m_le_find->text();
        if (!find_text.isEmpty()) {
            emit signalFindPrevious(find_text, m_is_case_sensitive, m_is_whole_word);
        }
    }
        break;
    default:
        break;
    }
}

void MessageToolBar::slotOnFindTextChanged(const QString &text)
{
    if (m_find_text != text && !m_find_text.isEmpty()) {
        m_find_text.clear();
        emit signalClearFindResult();
    }
}

void MessageToolBar::slotOnFindTextEditFinished()
{
    QString find_text = m_le_find->text();
    if (!find_text.isEmpty()) {
        m_find_text = find_text;
        emit signalFind(find_text, m_is_case_sensitive, m_is_whole_word);
    }
}


} // namespace Domy
